package uit.mmt02.activity;

import java.util.ArrayList;
import java.util.List;

import javax.mail.Message;

import uit.mmt02.R;
import uit.mmt02.share.ConvertMessageToList;
import uit.mmt02.share.DB_EmailLoginManager_Adapter;
import uit.mmt02.share.OneMail;
import uit.mmt02.share.myadapter;
import uit.mmt02.share.showToast;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class getGmailActivity extends Activity // hien thi mail tren 1 list
{
	final static String tag   = "getGmailActivity";
	uit.mmt02.gmail.GmailControl g ;
	
	//---------------------------------Copy...
	
	final static String KEY_USERNAME_LOGIN = "USERNAMELOGIN"; // nhan USERVA PASS tu showmail activity gui toi de login
	final static String KEY_PASSWORD_LOGIN = "PASSWORDLOGIN";
	static final String FROM = "from";
	static final String BODY = "body";
	static final String SUB = "sub";
	static final String DATE = "date";
	static final String TIME = "time";
	static final String TO = "to";
	//option......................
	static final String ATTACH = "attach";
	static final String VOICE = "voice";
	static final String ENCRYPT = "encrypt";
	String username;
	String password;
	ArrayAdapter<OneMail>adapter;
	Message[]message;
	showToast d;
	List<OneMail>list; //list truyen vao adapter
	int n ; // so mail
	Bundle bundle; //nhan username va password
	ListView listview; //lay listview
	View view; // view footer
	LayoutInflater Linflater;
	Button btn_more, btn_logout;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_email_login);
		listview = (ListView)findViewById(R.id.listview_list_email);
		d = new showToast(getBaseContext());
		list = new ArrayList<OneMail>();	
		//
		Linflater = getLayoutInflater();
		view = Linflater.inflate(R.layout.footer_list_mail, null);
		//
		bundle = getIntent().getExtras();
		username = bundle.getString(KEY_USERNAME_LOGIN);
		password = bundle.getString(KEY_PASSWORD_LOGIN);
		Log.v(tag, "Nhan duoc : " + username + " | " + password);
		//goi class loadmail chay nhu 1 tien trinh moi
		new loadgmail().execute(tag);
		//-------------
		btn_more = (Button)view.findViewById(R.id.btn_foot_more);
		btn_more.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				final ProgressDialog pdialog1 = new ProgressDialog(getGmailActivity.this);
//				pdialog1.setMessage("Đang tải ....");
//				pdialog1.show();
//				new load_more_mail().execute("more");
//				d.showmess("mor mire");
				add_more_email();
//				pdialog1.dismiss();
//				new Runnable() {
//					public void run() {
//						add_more_email();
//						adapter.notifyDataSetChanged();
//						pdialog1.dismiss();
//					}
//				}.run();
//				adapter.notifyDataSetChanged();
//				new Thread(new Runnable() {
//					
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						add_more_email();
//						adapter.notifyDataSetChanged();
//						pdialog1.dismiss();
//						
//					}
//				}).start();
			}
		});
		
		btn_logout = (Button)view.findViewById(R.id.btn_foot_logout);
		btn_logout.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO REMOVE USERNAME NAY RA KHOI CSDL
				d.showmess("logout ...");
				DB_EmailLoginManager_Adapter mydb = new DB_EmailLoginManager_Adapter(getGmailActivity.this);
				mydb.open();
				mydb.remove_by_username(username);
				mydb.close();
				startActivity(new Intent(getGmailActivity.this,  uit.mmt02.activity.ListMailActivity.class));
				
			}
		});
		
		TextView txt_email = (TextView)view.findViewById(R.id.txt_foot_email);
		txt_email.setText(username);
	}
	
	// asyncTask Load mail
		private class loadgmail extends AsyncTask<String,String	, Message[]>
		{
			ProgressDialog pdialog = new ProgressDialog(getGmailActivity.this);
			
			@Override
			protected void onPreExecute() 
			{
//				pdialog.setTitle("Get Mail");
				pdialog.setMessage("Đang tải email từ : " + username);
				pdialog.show();
				super.onPreExecute();
			}

			@Override
			protected Message[] doInBackground(String... params) 
			{
				// TODO Auto-generated method stub
				Log.v(tag, "doinBackGroud");
				g = new uit.mmt02.gmail.GmailControl(username, password);
				message = g.getmail();
				ConvertMessageToList cv = new ConvertMessageToList();
				list = cv.convert2(message);
				adapter = new myadapter(getGmailActivity.this, list);
//				LayoutInflater inflater = getLayoutInflater();
//				view = inflater.inflate(R.layout.footer_list_mail, null);
				Log.v(tag, "end doinBackGroud");
				return message;
				
			}
			@Override
			protected void onPostExecute(Message[] resultMes) {
				// TODO Auto-generated method stub
				Log.v(tag,"on post");
				
				listview.addFooterView(view);
				listview.setAdapter(adapter);
				pdialog.dismiss();
				
				//
//				Button btn_more = (Button)view.findViewById(R.id.btn_foot_more);
//				btn_more.setOnClickListener(new View.OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						new load_more_mail().execute("more");
//					}
//				});
//				
//				Button btn_logout = (Button)view.findViewById(R.id.btn_foot_logout);
//				btn_logout.setOnClickListener(new View.OnClickListener() {
//					
//					@Override
//					public void onClick(View v) {
//						// TODO REMOVE USERNAME NAY RA KHOI CSDL
//						d.showmess("logout ...");
//						
//					}
//				});
//				
//				TextView txt_email = (TextView)view.findViewById(R.id.txt_foot_email);
//				txt_email.setText(username);
				//
				super.onPostExecute(resultMes);
			}
			
		}
		
		
		//menu
		// tao menu 
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// TODO Auto-generated method stub
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.layout_menu, menu);
			return super.onCreateOptionsMenu(menu);
		}
		//khi click vao menu item
		@Override
		public boolean onOptionsItemSelected(MenuItem item) 
		{
			// TODO Auto-generated method stub
			switch (item.getItemId()) 
			{
			case R.id.item_newmail:
				d.showmess("new mail");
				Intent i = new Intent("uit.mmt02.activity.newmailActivity");
				startActivity(i);
				break;
			case R.id.item_sortby:
				d.showmess("sort by");
				break;
			case R.id.item_selectAll:
				d.showmess("selected all");
				break;
			case R.id.item_check:
				d.showmess("check mail again");
				break;
			case R.id.item_more:
				d.showmess("more");
				break;

			default:
				break;
			}
			return super.onOptionsItemSelected(item);
		}
		
		//ham add them 5 email vao list
		private void add_more_email() {
			// TODO Auto-generated method stub
			try
			{
				n = list.size(); // so mail da dc load
				int n_tongmail = message.length; // tong so mail
				int n_batdautai = n_tongmail -n-1;
				Log.v(tag, "get dc " + n_tongmail + "get tu " + n_batdautai);
				ConvertMessageToList cv2 = new ConvertMessageToList();
//				for (int i = 10 ; i > 5 ; i--)
				for (int i = n_batdautai ; i > n_batdautai-5 ; i--)
				{
					if(i >= 0)
					{
						OneMail om = cv2.get1mail(message, i);
						adapter.add(om);
						Log.v(tag, "get dc " + i);
					}
					
				}
//				adapter.notifyDataSetChanged();
			}
			
			catch (Exception e) {
				// TODO: handle exception
				Log.i(tag, "Loi add 5 email" + e.toString());
			}

		}
		
		//tao 1 tien trinh load them email rieng, 1 dialog bao ng dung doi
		private class load_more_mail extends AsyncTask<String, String, String>
		{
			ProgressDialog pdialog = new ProgressDialog(getGmailActivity.this);
			
			@Override
			protected void onPreExecute() 
			{
//				pdialog.setTitle("Get Mail");
				pdialog.setMessage("Đang tải ....");
				pdialog.show();
				super.onPreExecute();
			}

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
			
				
				new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						add_more_email();
						
					}
				};
//				adapter.notifyDataSetChanged();
//				pdialog.dismiss();
				return null;
			}
			@Override
			protected void onPostExecute(String result) {
				// TODO Auto-generated method stub
//				adapter.notifyDataSetChanged();
				pdialog.dismiss();
				super.onPostExecute(result);
				
			}
			
		}
		
}
